<!--
 * @Author       : wfl
 * @LastEditors: qiye
 * @description  :
 * @updateInfo   :
 * @Date         : 2022-11-01 10:07:47
 * @LastEditTime: 2024-02-04 17:55:32
-->
<script lang="ts" setup name="DepTable">
import { _, ikTree } from 'iking-utils'
import type { Ref } from 'vue'
import { useFields } from './fields'
import { depApi } from '@/MainApp/apis/dep'

const { msgError } = useMessage()
const selectDepNode: Ref<any> | undefined = inject('selectDepNode')
const { fieldList } = useFields()

watch(
  () => selectDepNode?.value,
  val => {
    if (val)
      handChild()
  },
  { deep: true }
)

const handDepTree = async () => {
  // const { success, msg, data } = await depApi.getDepTree()
}
handDepTree()

const depData = ref([])
const handChild = async () => {
  const { success, msg, data } = await depApi.getChildDep(selectDepNode?.value?.id)
  success && (depData.value = data)
  if (!success) {
    msgError(msg)
    depData.value = []
  }
  depData.value = ikTree.listToTree(_.cloneDeep(depData.value), { sort: 'sortOrder', needSort: true })
}
</script>

<template>
  <IkPageFull
    :serial="false"
    :table-data="depData"
    :fields="fieldList"
    highlight-current-row
    default-expand-all
    header-border
    row-key="id"
    header-title="部门列表"
    :search="handChild"
  >
    <template #manager="{ data }">
      {{ data.row.manager?.userName }}
    </template>
  </IkPageFull>
</template>
